/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.Component;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;

/**
 *
 * @author 0
 */
public class JErrorProvider {
	Vector<JComponent> ctrl = new Vector<JComponent>();
	Icon icon = new ImageIcon("src\\img\\warn.png");
	boolean sound = true;
	int blinkrate = 250;
        boolean blink = true;

        public boolean isBlink() {
            return blink;
        }

        public void setBlink(boolean blink) {
            this.blink = blink;
        }
        
        public int getBlinkRate() {
            return blinkrate;
        }

        public void setBlinkRate(int blinkrate) {
            this.blinkrate = blinkrate;
        }
	
	public void setIcon(Icon icon) {
		this.icon = icon;
	}

        public Icon getIcon() {
            return icon;
        }
	
	public void setSound(boolean b) {
		this.sound = b;
	}
	
	public boolean isSound() {
		return sound;
	}
	
	public void setError(JComponent control, String msg) {
		
		JComponent parent = (JComponent) control.getParent();
		Rectangle rec = control.getBounds();
			int w = rec.width;
			//int h = rec.height;
			int x = rec.x;
			int y = rec.y;
		
		if(!this.ctrl.contains(control)) { // if doesn't already exist.
			this.ctrl.add(control);        // add to Vector.
			
			JLabel lblerror = new JLabel();
				lblerror.setBounds(new Rectangle((x + w + 5), y + 2, 16, 16));
				lblerror.setIcon(this.icon);
				lblerror.setToolTipText(msg);
			
			parent.add(lblerror);
			setBlinkDisplay(lblerror);
			setDisplay(parent);
		}
		else { // is already exist.
			   // just reuse.
			JLabel lblerror = (JLabel) parent.getComponentAt((x + w + 5), y + 2);
			setBlinkDisplay(lblerror);
			lblerror.setToolTipText(msg);
		}
		
		if(sound) Toolkit.getDefaultToolkit().beep();
	}
	
	public String getErrorMessage(JComponent control) {
		
		try {
			JLabel lblerror = (JLabel) getErrorLabel(control);
			return lblerror.getToolTipText();
		}
		catch(Exception ex) {
			return null;
		}
	}
	
	public void clear() {
		
		if(ctrl.size() > 0) {
			JComponent parent = null;
			
			for(int i=0;i<ctrl.size();i++) {
				parent = (JComponent) ctrl.get(i).getParent();
				parent.remove(
					getErrorLabel(ctrl.get(i))
				);
			}
			ctrl.clear();
			setDisplay(parent);
		}
	}
	
	public void clear(JComponent control) {
		if(ctrl.contains(control)) {
			JComponent parent = (JComponent) control.getParent();
				parent.remove(getErrorLabel(control));
			
			ctrl.remove(control);
			ctrl.trimToSize();
			setDisplay(parent);
		}
	}
	
	private Component getErrorLabel(JComponent jcom) {
		JComponent parent = (JComponent) jcom.getParent();
		Rectangle rec = jcom.getBounds();
			int w = rec.width;
			int x = rec.x;
			int y = rec.y;
			
		return parent.getComponentAt((x + w + 5), y + 2);
	}
	
	private void setBlinkDisplay(JComponent jcom) {
		if(blink) new Blink(jcom);
	}
	
	private void setDisplay(JComponent jcom) {
		jcom.revalidate();
		jcom.repaint();
	}
	
	class Blink implements Runnable {

		private JComponent lblerror = null;
		private Thread newThread = null;

			Blink(JComponent jcom) {
				this.lblerror = jcom;
				newThread = new Thread(this);
				newThread.start();
			}

			@Override
			public void run() {
				// TODO Auto-generated method stub
				for(int i=0;i<3;i++) {
					
					try {
						Thread.sleep(
							blinkrate
						);
						lblerror.setVisible(false);
						
						Thread.sleep(
						   (int) Math.floor(blinkrate / 2)
						);
						lblerror.setVisible(true);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
	}
}